<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;


use app\dao\car\CarHolidayDao;
use app\model\car\CarHoliday;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;

/**
 * Class CarHolidayServices
 * @package app\services\car
 */
class CarHolidayServices extends BaseServices
{
    public function __construct(CarHolidayDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param string $field
     * @param bool $is_page
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(array $where = [], string $field = '*', bool $is_page = false){
        $page = $limit = 0;
        if($is_page){
            [$page,$limit] = $this->getPageValue();
        }
        $list = $this->dao->getList($where,$field,$page,$limit);
        foreach ($list['list'] as &$item){
            $item['begin_time'] = date('Y-m-d',$item['begin_time']);
            $item['end_time'] = date('Y-m-d',$item['end_time']);
        }
        return $list;
    }

    /**
     * 添加/修改
     * @param int $id
     * @param array $data
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(int $id,array $data){
        $data['begin_time'] = $begin_time = strtotime($data['begin_time']);
        $data['end_time'] = $end_time = strtotime($data['end_time'].' 23:59:59');
        if($id){
            if($this->dao->update($id,$data)){
                return '修改成功';
            }else{
                throw new AdminException('修改失败或您什么也没修改');
            }
        }else{
            if($begin_time > $end_time) throw new AdminException('开始时间不能大于结束时间');
            // 判断假期有效性
            $day = strtotime(date('Y-m-d',time()));
            if($begin_time < $day || $end_time < $day){
                throw new AdminException('请设置有效假期时间');
            }
            // 找出该门店相关的假期
            $place_holiday = CarHoliday::where(['store_id'=>$data['store_id'],'is_del'=>0])->field('begin_time,end_time')->select()->toArray();
            // 判断假期是否有重叠
            foreach ($place_holiday as $item){
                if($item['begin_time']<=$begin_time && $item['end_time']>=$begin_time){// 假期开始时间是否重叠
                    throw new AdminException('假期开始时间被其它假期占用');
                }
                if($item['begin_time']<=$end_time && $item['end_time']>=$end_time){// 假期结束时间是否重叠
                    throw new AdminException('假期结束时间被其它假期占用');
                }
            }
            unset($data['id']);
            $data['add_time'] = time();
            if($this->dao->save($data)){
                return '添加成功';
            }else{
                throw new AdminException('添加失败');
            }
        }
    }

    /**
     * 假期详情
     * @param $data
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getHolidayInfo($data){
        $holiday = $this->dao->get($data['id']);
        if(!$holiday) throw new AdminException('未查询到该数据');
        $holiday['begin_time'] = date('Y-m-d',$holiday['begin_time']);
        $holiday['end_time'] = date('Y-m-d',$holiday['end_time']);
        return $holiday->toArray();
    }

    /**
     * 删除假期
     * @param int $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delHoliday(int $id){
        $type = $this->dao->get($id);
        if ($type) {
            $data['is_del'] = 1;
            $data['delete_time'] = time();
            if (!$this->dao->update($id,$data)) {
                throw new AdminException('删除失败,请稍候再试!');
            }
        }
        return '删除成功!';
    }
}
